package com.ufo.store.management.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.security.MessageDigest;

public class GlideUtils {
    /**
     * 加载网络图片
     *
     * @param context
     * @param url
     * @param imageview
     */
    public static void LoadImage(Context context, String url,
                                 ImageView imageview) {
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        Glide.with(context).load(url).apply(options).into(imageview);
    }

    /**
     * 加载带尺寸的图片
     *
     * @param context
     * @param url
     * @param Width
     * @param Height
     * @param imageview
     */
    public static void LoadImageWithSize(Context context, String url,
                                         int Width, int Height, ImageView imageview) {
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        options.override(Width, Height);
        Glide.with(context).load(url).apply(options).into(imageview);
    }

    /**
     * 加载本地图片
     *
     * @param context
     * @param path
     * @param imageview
     */
    public static void LoadImageWithLocation(Context context, String path,
                                             ImageView imageview) {
        File file = new File(path);
        if( file.exists()) {
            RequestOptions options = new RequestOptions();
            options.diskCacheStrategy(DiskCacheStrategy.NONE);
            options.skipMemoryCache(true);
            Glide.with(context).load(file).apply(options).into(imageview);
        }
    }

    /**
     * 加载本地图片
     *
     * @param context
     * @param resourceId
     * @param imageview
     */
    public static void LoadImageWithResource(Context context, int resourceId,
                                             ImageView imageview) {
        Glide.with(context).load(resourceId).into(imageview);
    }

    /**
     * 圆形加载
     *
     * @param context
     * @param url
     * @param imageview
     */
    public static void LoadCircleImage(Context context, String url,
                                       ImageView imageview) {
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        options.transform(new GlideCircleTransform(context));
        Glide.with(context).load(url).apply(options).into(imageview);
    }


    /**
     * 圆角加载
     *
     * @param context
     * @param url
     * @param dp
     * @param imageview
     */
    public static void LoadRoundImage(Context context, String url, int dp,
                                      ImageView imageview) {
        RequestOptions options = new RequestOptions();
        options.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        options.transform(new GlideRoundTransform(context, dp));
        Glide.with(context).load(url).apply(options).into(imageview);
    }

    /**
     * 圆形图片
     */
    public static class GlideCircleTransform extends BitmapTransformation {
        public GlideCircleTransform(Context context) {
        }

        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return circleCrop(pool, toTransform);
        }

        private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
            int size = (int) (Math.min(source.getWidth(), source.getHeight()));
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
            // TODO this could be acquired from the pool too
            Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
            Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            }
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);
            return result;
        }

        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

        }
    }

    /**
     * 圆角图片
     */
    public static class GlideRoundTransform extends BitmapTransformation {
        private static float radius = 0f;

        /**
         * 构造函数
         *
         * @param context Context
         * @param dp      圆角半径
         */
        public GlideRoundTransform(Context context, int dp) {
            radius = Resources.getSystem().getDisplayMetrics().density * dp;
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return roundCrop(pool, toTransform);
        }

        private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            }
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
        }

        @Override
        public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

        }
    }
}
